#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct stu
{
	char name[20];
	int age;
};
int cmp_str(const void* p1, const void* p2)
{
	return strcmp(((struct stu*)p1)->name, ((struct stu*)p2)->name);
}
int swap(char* buf1, char* buf2, int width)
{
	int i = 0;
	for (; i < width; i++)
	{
		char tmp = *buf1;
		*buf1 = *buf2;
		*buf2 = tmp;
		buf1++;
		buf2++;
	}
}
void my_qsort(void* base, size_t sz, size_t width, int(*cmp)(const void* p1, const void* p2))
{
	int i = 0;
	for (; i < sz - 1; i++)
	{
		int j = 0;
		for (; j < sz - i - 1; j++)
		{
			if (cmp((char*)base + j * width, (char*)base + (j + 1) * width) > 0)
				swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
		}
	}
}
int cmp(const void* p1, const void* p2);
int main()
{
	struct stu memb[] = { {"zhangsan",18} ,{"lisi",19}, {"wangwu",20} };
	int sz = sizeof(memb) / sizeof(memb[0]);
	my_qsort(memb, sz, sizeof(memb[0]), cmp_str);
	for (int j = 0; j < sz; j++)
	{
		printf("%s %d \n", memb[j].name,memb[j].age);
	}
	
	//other knowledge
	int arr[] = { 0,1,2,3,4,5,6,7,8,9 };
	int sz_ = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz_, sizeof(arr[0]),cmp);
	for (int i = 0; i < sz_; i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}
int cmp(const void* p1, const void* p2)
{
	return *(int*)p1 - *(int*)p2;
}